package controllers;

import com.ebay.sdk.ApiException;
import com.ebay.sdk.SdkException;
import com.ebay.services.finding.ProductId;
import com.ebay.services.finding.SearchItem;
import com.ebay.soap.eBLBaseComponents.AmountType;
import com.ebay.soap.eBLBaseComponents.AttributeArrayType;
import com.ebay.soap.eBLBaseComponents.CatalogProductType;
import com.ebay.soap.eBLBaseComponents.DataElementSetType;
import com.ebay.soap.eBLBaseComponents.EndReasonCodeType;
import com.ebay.soap.eBLBaseComponents.ItemArrayType;
import com.ebay.soap.eBLBaseComponents.ItemType;
import com.ebay.soap.eBLBaseComponents.ListingDurationCodeType;
import com.ebay.soap.eBLBaseComponents.PictureDetailsType;
import com.ebay.soap.eBLBaseComponents.ProductListingDetailsType;
import java.lang.reflect.*;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import play.mvc.*;

public class TestApi extends Controller {

	public static void index() {
		try {
			Class c = TestApi.class; // Class.forName("TestApi");
			Method m[] = c.getDeclaredMethods();
			ArrayList<String> methodNames = new ArrayList<String>();
			for (int i = 1; i < m.length; i++) {
				methodNames.add(m[i].getName());
			}
			render(methodNames);
		} catch (Throwable e) {
			System.err.println(e);
		}
	}

	public static void getTimeString() {
		Api api = new Api();
		renderText(api.getTimeString());
	}

	public static void addMathBook() throws ApiException, SdkException,
			Exception {
		Api api = new Api();
		double price = 14.99;
		double shippingPrice = 2.00;
		ListingDurationCodeType duration = ListingDurationCodeType.DAYS_30;
		String itemId = api.addItem("Math and Physics", "9780307473479", "my math book",
				price, shippingPrice, duration);
		renderText("You just listed item id: " + itemId);
	}

	public static void addRandomItem() throws ApiException, SdkException,
			Exception {
		Api api = new Api();
		double price = 14.99;
		double shippingPrice = 0.0;
		ListingDurationCodeType duration = ListingDurationCodeType.DAYS_30;
		SecureRandom random = new SecureRandom();
		String name = new BigInteger(130, random).toString(32);
		String itemId = api.addItem(name, "9780307473479",
				"my randomly named book", price, shippingPrice, duration);
		renderText("You just listed item id: " + itemId);
	}

	public static void viewItem() throws ApiException, SdkException, Exception {
		Api api = new Api();
		ItemType item = api.viewItem("110094768513");
		String output = "Title: " + item.getTitle() + " \n";
		output += "Description: " + item.getDescription() + " \n";
		output += "ItemID: " + item.getItemID() + " \n";
		output += "Ebay URL: http://cgi.sandbox.ebay.com/" + item.getItemID()
				+ " \n";
		output += "Location: " + item.getLocation() + " \n";
		output += "Buy it Now Price: " + item.getBuyItNowPrice().getValue()
				+ " \n";

		PictureDetailsType pictureDetails = item.getPictureDetails();
		output += "Picture URL: " + pictureDetails.getPictureURL().toString()
				+ " \n\n";

		ProductListingDetailsType details = item.getProductListingDetails();

		if (details != null) {
			output += "ISBN: " + details.getISBN() + " \n";
			output += "Details URL: " + details.getDetailsURL() + " \n";
			output += "Product Details URL: " + details.getProductDetailsURL()
					+ " \n";
			output += "Stock Photo URL: " + details.getStockPhotoURL() + " \n";
		} else {
			output += "product details are null";
		}

		renderText(output);
	}

	public static void getAllCurrentItems() throws ApiException, SdkException,
			Exception {
		Api api = new Api();
		ItemType[] items = api.getAllCurrentItems();
		
		String output = "";
		for (ItemType item : items) {
			item = api.viewItem(item.getItemID());		
			item.setBuyItNowPrice(item.getStartPrice());
			output += "Title: ";
			output += item.getTitle() + "\n";
			output += "ID: ";
			output += item.getItemID() + "\n";
			output += "ISBN: " + item.getProductListingDetails().getISBN() + "\n";
			output += "------------------\n\n";
		}
		renderText(output);
	}

	public static void removeItem() throws ApiException, SdkException,
			Exception {
		Api api = new Api();
		String itemIDStr = "110094752492";
		api.removeItem(itemIDStr, EndReasonCodeType.NOT_AVAILABLE);
		renderText("Item Removed: " + itemIDStr);
	}

	public static void searchProductsByKeyword() throws ApiException,
			SdkException, Exception {
		Api api = new Api();
		String keyword = "110094848540";
		String output = "";
		if (api.searchProductsByKeywords(keyword) == null) {
			renderText("it dose not work");
		} else {
			// renderText("it works");
			CatalogProductType[] catalogProductTypes = api
					.searchProductsByKeywords(keyword);
			for (CatalogProductType catalogProductType : catalogProductTypes) {
				output += "Title: ";
				output += catalogProductType.getTitle() + "\n";
				output += "URL: ";
				output += catalogProductType.getStockPhotoURL() + "\n";
				output += "ISBN: ";
				if (catalogProductType.getExternalProductID() != null) {
					output += catalogProductType.getExternalProductID()
							.getValue()
							+ "\n";
				}
				if(catalogProductType.getItemSpecifics()!=null){
				output+= "TEST:==="+catalogProductType.getItemSpecifics().getNameValueList(0).getValue(0)+"\n";
				}
			}
			
			output += "There ara " + catalogProductTypes.length + " result";
			renderText(output);

		}

	}

	public static void searchProductsByReferenceID() throws ApiException,
			SdkException, Exception {
		Api api = new Api();
		String referenceID = "71235308";
		String output = "";
		if (api.searchProductsByKeywords(referenceID) == null) {
			renderText("it dose not work");
		} else {
			// renderText("it works");
			CatalogProductType[] catalogProductTypes = api
					.searchProductsByKeywords(referenceID);
			for (CatalogProductType catalogProductType : catalogProductTypes) {
				output += "Title: ";
				output += catalogProductType.getTitle() + "\n";
				output += "URL: ";
				output += catalogProductType.getStockPhotoURL() + "\n";
				output += "ISBN: ";
				if (catalogProductType.getExternalProductID() != null) {
					output += catalogProductType.getExternalProductID().getValue() + "\n";
				}
			}
			output += "There ara " + catalogProductTypes.length + " result";
			renderText(output);

		}

	}

	public static void searchProductsByISBN() throws ApiException,
			SdkException, Exception {
		Api api = new Api();
		String ISBN = "0590353403";
		String output = "";
		if (api.searchProductsByKeywords(ISBN) == null) {
			renderText("it dose not work");
		} else {
			// renderText("it works");
			ItemArrayType itemArrayType = api.searchProductsByISBN(ISBN);
			ItemType[] items = itemArrayType.getItem();
			for (ItemType item : items) {
				output += "Title: " + item.getTitle() + "\n";
				output += "Location: " + item.getLocation() + "\n";

			}
			renderText(output);
		}
	}

	public static void ericSearchProducts() throws ApiException, SdkException,
			Exception {
		Api api = new Api();
		String keyword = "Harry Potter";

		int numResults = api.ericSearchProductsByKeywords(keyword);

		renderText("Number of results: " + numResults);
	}

	public static void buyerSearchFindingAPI() {
		Api api = new Api();
		String output = "";
		 ProductId id = new ProductId();
	     id.setType("ISBN");  
		 List<SearchItem> results = api.buyerSearchByKeywordFindingApi("Math");
		if (results != null) {
			for (SearchItem result : results) {
				result.setProductId(id);		       
				output += "Title: " + result.getTitle() + "\n";
				output += "Item ID: " + result.getItemId() + "\n";
				output += "URL: " + result.getGalleryURL() + "\n";
				output += "ViewItemURL:" + result.getViewItemURL() + "\n";
				if (result.getProductId() != null) {
					output += "ISBN:" + result.getProductId().getValue() + "\n";
				}
				if (result.getListingInfo().getBuyItNowPrice() != null) {
					output += "BuyItNowPrice:"
							+ result.getListingInfo().getBuyItNowPrice()
									.getValue() + "\n";
				}
				output += "ShippingServiceCost:"
						+ result.getShippingInfo().getShippingServiceCost()
								.getValue() + "\n";
				if (result.getSellerInfo() != null) {
					output += "Seller: "
							+ result.getSellerInfo().getSellerUserName()
							+ "\n\n";
				}
			}
			renderText(output);
		} else
			renderText("Can not find anything");
	}
}
